﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace XDSoft.DAL
{
	/// <summary>
	/// 数据访问类:TB_Clients
	/// </summary>
	public partial class TB_Clients
	{
		public TB_Clients()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("id", "TB_Clients"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from TB_Clients");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(XDSoft.Model.TB_Clients model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into TB_Clients(");
			strSql.Append("clientName,corpName,address,tel,mobile1,mobile2,webUrl,email,qq,traceLog,memo,clientLevel)");
			strSql.Append(" values (");
			strSql.Append("@clientName,@corpName,@address,@tel,@mobile1,@mobile2,@webUrl,@email,@qq,@traceLog,@memo,@clientLevel)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@clientName", SqlDbType.VarChar,50),
					new SqlParameter("@corpName", SqlDbType.VarChar,100),
					new SqlParameter("@address", SqlDbType.VarChar,100),
					new SqlParameter("@tel", SqlDbType.VarChar,20),
					new SqlParameter("@mobile1", SqlDbType.VarChar,20),
					new SqlParameter("@mobile2", SqlDbType.VarChar,20),
					new SqlParameter("@webUrl", SqlDbType.VarChar,100),
					new SqlParameter("@email", SqlDbType.VarChar,50),
					new SqlParameter("@qq", SqlDbType.VarChar,20),
					new SqlParameter("@traceLog", SqlDbType.VarChar,300),
					new SqlParameter("@memo", SqlDbType.VarChar,200),
					new SqlParameter("@clientLevel", SqlDbType.VarChar,20)};
			parameters[0].Value = model.clientName;
			parameters[1].Value = model.corpName;
			parameters[2].Value = model.address;
			parameters[3].Value = model.tel;
			parameters[4].Value = model.mobile1;
			parameters[5].Value = model.mobile2;
			parameters[6].Value = model.webUrl;
			parameters[7].Value = model.email;
			parameters[8].Value = model.qq;
			parameters[9].Value = model.traceLog;
			parameters[10].Value = model.memo;
			parameters[11].Value = model.clientLevel;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(XDSoft.Model.TB_Clients model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update TB_Clients set ");
			strSql.Append("clientName=@clientName,");
			strSql.Append("corpName=@corpName,");
			strSql.Append("address=@address,");
			strSql.Append("tel=@tel,");
			strSql.Append("mobile1=@mobile1,");
			strSql.Append("mobile2=@mobile2,");
			strSql.Append("webUrl=@webUrl,");
			strSql.Append("email=@email,");
			strSql.Append("qq=@qq,");
			strSql.Append("traceLog=@traceLog,");
			strSql.Append("memo=@memo,");
			strSql.Append("clientLevel=@clientLevel");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@clientName", SqlDbType.VarChar,50),
					new SqlParameter("@corpName", SqlDbType.VarChar,100),
					new SqlParameter("@address", SqlDbType.VarChar,100),
					new SqlParameter("@tel", SqlDbType.VarChar,20),
					new SqlParameter("@mobile1", SqlDbType.VarChar,20),
					new SqlParameter("@mobile2", SqlDbType.VarChar,20),
					new SqlParameter("@webUrl", SqlDbType.VarChar,100),
					new SqlParameter("@email", SqlDbType.VarChar,50),
					new SqlParameter("@qq", SqlDbType.VarChar,20),
					new SqlParameter("@traceLog", SqlDbType.VarChar,300),
					new SqlParameter("@memo", SqlDbType.VarChar,200),
					new SqlParameter("@clientLevel", SqlDbType.VarChar,20),
					new SqlParameter("@id", SqlDbType.Int,4)};
			parameters[0].Value = model.clientName;
			parameters[1].Value = model.corpName;
			parameters[2].Value = model.address;
			parameters[3].Value = model.tel;
			parameters[4].Value = model.mobile1;
			parameters[5].Value = model.mobile2;
			parameters[6].Value = model.webUrl;
			parameters[7].Value = model.email;
			parameters[8].Value = model.qq;
			parameters[9].Value = model.traceLog;
			parameters[10].Value = model.memo;
			parameters[11].Value = model.clientLevel;
			parameters[12].Value = model.id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from TB_Clients ");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from TB_Clients ");
			strSql.Append(" where id in ("+idlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public XDSoft.Model.TB_Clients GetModel(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 id,clientName,corpName,address,tel,mobile1,mobile2,webUrl,email,qq,traceLog,memo,clientLevel from TB_Clients ");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			XDSoft.Model.TB_Clients model=new XDSoft.Model.TB_Clients();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["id"]!=null && ds.Tables[0].Rows[0]["id"].ToString()!="")
				{
					model.id=int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["clientName"]!=null && ds.Tables[0].Rows[0]["clientName"].ToString()!="")
				{
					model.clientName=ds.Tables[0].Rows[0]["clientName"].ToString();
				}
				if(ds.Tables[0].Rows[0]["corpName"]!=null && ds.Tables[0].Rows[0]["corpName"].ToString()!="")
				{
					model.corpName=ds.Tables[0].Rows[0]["corpName"].ToString();
				}
				if(ds.Tables[0].Rows[0]["address"]!=null && ds.Tables[0].Rows[0]["address"].ToString()!="")
				{
					model.address=ds.Tables[0].Rows[0]["address"].ToString();
				}
				if(ds.Tables[0].Rows[0]["tel"]!=null && ds.Tables[0].Rows[0]["tel"].ToString()!="")
				{
					model.tel=ds.Tables[0].Rows[0]["tel"].ToString();
				}
				if(ds.Tables[0].Rows[0]["mobile1"]!=null && ds.Tables[0].Rows[0]["mobile1"].ToString()!="")
				{
					model.mobile1=ds.Tables[0].Rows[0]["mobile1"].ToString();
				}
				if(ds.Tables[0].Rows[0]["mobile2"]!=null && ds.Tables[0].Rows[0]["mobile2"].ToString()!="")
				{
					model.mobile2=ds.Tables[0].Rows[0]["mobile2"].ToString();
				}
				if(ds.Tables[0].Rows[0]["webUrl"]!=null && ds.Tables[0].Rows[0]["webUrl"].ToString()!="")
				{
					model.webUrl=ds.Tables[0].Rows[0]["webUrl"].ToString();
				}
				if(ds.Tables[0].Rows[0]["email"]!=null && ds.Tables[0].Rows[0]["email"].ToString()!="")
				{
					model.email=ds.Tables[0].Rows[0]["email"].ToString();
				}
				if(ds.Tables[0].Rows[0]["qq"]!=null && ds.Tables[0].Rows[0]["qq"].ToString()!="")
				{
					model.qq=ds.Tables[0].Rows[0]["qq"].ToString();
				}
				if(ds.Tables[0].Rows[0]["traceLog"]!=null && ds.Tables[0].Rows[0]["traceLog"].ToString()!="")
				{
					model.traceLog=ds.Tables[0].Rows[0]["traceLog"].ToString();
				}
				if(ds.Tables[0].Rows[0]["memo"]!=null && ds.Tables[0].Rows[0]["memo"].ToString()!="")
				{
					model.memo=ds.Tables[0].Rows[0]["memo"].ToString();
				}
				if(ds.Tables[0].Rows[0]["clientLevel"]!=null && ds.Tables[0].Rows[0]["clientLevel"].ToString()!="")
				{
					model.clientLevel=ds.Tables[0].Rows[0]["clientLevel"].ToString();
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,clientName,corpName,address,tel,mobile1,mobile2,webUrl,email,qq,traceLog,memo,clientLevel ");
			strSql.Append(" FROM TB_Clients ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" id,clientName,corpName,address,tel,mobile1,mobile2,webUrl,email,qq,traceLog,memo,clientLevel ");
			strSql.Append(" FROM TB_Clients ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM TB_Clients ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.id desc");
			}
			strSql.Append(")AS Row, T.*  from TB_Clients T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "TB_Clients";
			parameters[1].Value = "id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

